//先引入系统库
const fs = require('fs')

//引入第三方库
const request = require('syncrequest')
const cheerio = require('cheerio')

//引入自己写的库
const log = console.log.bind(console)

class Movies {
    constuctor() {
        name: ''
    }
}

const fromDiv = (div) => {
    //此处有点不理解 又要转换一次数据
    let e = cheerio.load(div)
    return e('.title').text()
}

const ensurePath = () => {
    if(!fs.existsSync('./cacheUrl')) {
        fs.mkdirSync('./cacheUrl')
    }
}

const cacheUrl = (url) => {
    ensurePath()
    let path = url.split('?')[1]
    if (!fs.existsSync(`./cacheUrl/${path}`)) {
        let r = request.get.sync(url)
        let body = r.body
        fs.writeFileSync(`./cacheUrl/${path}`, body)
        return body
    } else {
        let data = fs.readFileSync(`./cacheUrl/${path}`)
        return data
    }

}

const fromUrl = (url) => {
    //下载网页,缓存判断
    let body = cacheUrl(url)
    //转化树状dom
    e = cheerio.load(body)
    //拿到元素数组
    let li = e('.item')
    //遍历数组，获取数据
    let results = []
    for (let m = 0; m < li.length; m++) {
       let div = li[m]
        //关键数据提取
       let result = fromDiv(div)
        results.push(result)
    }
    log('ee', results)
    return results
}

const main = () => {
    //1 下载网页
    //2 进行本地储存
    //3 内容提取
    let movies = []
    for (let i = 0; i < 10; i++) {
        let url = `https://movie.douban.com/top250?start=${i * 25}&filter=`
        let movie = fromUrl(url)
        movies.push(movie)
    }

    let data = JSON.stringify((movies), null, 2)

    fs.writeFileSync('./douban.json',data)

}


main()
